$(function () {
    vm.camaraTree();
    vm.shopTree();
    $("#jqGrid").jqGrid({
        url: baseURL + 'jxc/jxccash/list',
        datatype: "json",
        colModel: [
			{ label: '收银机编码', name: 'machineCode', index: 'machine_code', width: 80 }, 			
			{ label: '收银机名称', name: 'machineName', index: 'machine_name', width: 80 }, 			
			{ label: '商户名称', name: 'shopId', index: 'shop_id', width: 80 }, 			
			{ label: '收银监控相机', name: 'cameraId', index: 'camera_id', width: 80 }, 			
			{ label: '状态', name: 'status', index: 'status', width: 80,
                formatter: function(value, options, row) {
                    if (value == "0") {
                        return  '<span class="label label-success">正常</span>';
                    }
                    else if (value == "1") {
                        return    '<span class="label label-danger">禁用</span>';;
                    }
                }},
			{ label: '创建人', name: 'creator', index: 'creator', width: 80 }, 			
			{ label: '创建时间', name: 'createDate', index: 'create_date', width: 80 }, 			
			{ label: '更新人', name: 'updater', index: 'updater', width: 80 }, 			
			{ label: '更新时间', name: 'updateDate', index: 'update_date', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    },
    /*check: {
        enable: true ,//显示复选框
        chkStyle : "radio"
    },*/
};
var ztree;
var camara_setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    },
    /*check: {
        enable: true ,//显示复选框
        chkStyle : "radio"
    },*/
};
var camara_ztree;
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		jxcCash: {},
        q:{
            shopName:null,
            camaraName:null
        }
	},
	methods: {
	    shopTree:function(){
            //商户树
            $.get(baseURL + "jxc/jxccash/shopTree", function(r){
                ztree = $.fn.zTree.init($("#shopTree"), setting, r.shopTree);
                ztree.expandAll(true);
            })
        },
        camaraTree:function(){
            //监控摄像头组织树
            $.get(baseURL + "jxc/jxccash/camaraTree", function(r){
                camara_ztree = $.fn.zTree.init($("#camaraTree"), camara_setting, r.camaraTree);
                camara_ztree.expandAll(true);
            })
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
            $("#status").css("display","none");
			vm.jxcCash = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            $("#status").css("display","block");
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.jxcCash.id == null ? "jxc/jxccash/save" : "jxc/jxccash/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.jxcCash),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.q.shopName=null;
                             vm.q.camaraName=null;
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "jxc/jxccash/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "jxc/jxccash/info/"+id, function(r){
                vm.jxcCash = r.jxcCash;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
        relshopTree:function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择商户",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#shopLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var checkedNode=ztree.getSelectedNodes();//所选择节点
                    if(checkedNode[0].isClick){
                        vm.jxcCash.shopId =checkedNode[0].id;
                        vm.q.shopName =checkedNode[0].name;
                        layer.close(index);
                    }else{
                        alert("此处只能选择商户");
                    }
                }
            })
        },
        relcamaraTree:function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择监控",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#camaraLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var checkedNodes=camara_ztree.getSelectedNodes();//所选择节点
                    if(checkedNodes[0].isClick){
                        vm.jxcCash.camaraId =checkedNodes[0].id;
                        vm.q.camaraName =checkedNodes[0].name;
                        layer.close(index);
                    }else{
                        alert("此处只能选择监控");
                    }
                }
            })
        },
	}
});